package com.xianzhi.controller.content;

import com.xianzhi.constant.PageCodeEnum;
import com.xianzhi.dto.AdDto;
import com.xianzhi.service.AdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
//@ResponseBody
@RequestMapping("/ad")
public class AdController {
    @Autowired
    private AdService adService;
    @RequestMapping
    public String init(ModelMap modelMap){
        AdDto adDto = new AdDto();
        modelMap.addAttribute("list",adService.searchByPage(adDto));
        return "/content/adList";
    }

    @RequestMapping("/addInit")
    public String addInit() {
        return "/content/adAdd";
    }

    @RequestMapping("/add")
    public String add(AdDto adDto,ModelMap modelMap) {
        if(adService.add(adDto)) {
            modelMap.addAttribute(PageCodeEnum.KEY,PageCodeEnum.ADD_SUCCESS);
        }else {
            modelMap.addAttribute(PageCodeEnum.KEY,PageCodeEnum.ADD_FAIL);
        }
        return "/content/adAdd";
    }

    @RequestMapping("/search")
    public String search(ModelMap modelMap,AdDto adDto) {
        modelMap.addAttribute("list",adService.searchByPage(adDto));
        //jsp页面中的分页标签要使用Page类型，所以就可以通过adDto关联到Page对象
        modelMap.addAttribute("searchParam",adDto);
        return "/content/adList";
    }
}
